package com.dap.batch.controller;

import com.dap.batch.dao.IDaoService;
import com.dap.batch.entity.Result;
import com.dap.batch.enums.ResultEnum;
import com.dap.batch.exception.BatchCustomException;
import com.dap.batch.po.BatchJobPo;
import com.dap.batch.po.BatchStepPo;
import com.dap.batch.utils.ResultUtils;
import com.dap.dao.model.QueryResult;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @description: job控制类
 * @author: Fesine
 * @createTime:2017/9/27 11:02
 * @update:修改内容
 * @author: Fesine
 * @updateTime:2017/9/27 11:02
 */
@RestController
@RequestMapping("/v1")
public class BatchJobController {

    private final static Logger logger = LoggerFactory.getLogger(BatchJobController.class);

    private IDaoService daoService;

    @Autowired
    public BatchJobController(IDaoService daoService) {
        this.daoService = daoService;
    }

    /**
     * 根据任务组id查询所属任务
     * @param groupId
     * @param page
     * @param limit
     * @return
     */
    @ApiOperation(value = "获取当前任务组下任务信息列表", httpMethod = "GET")
    @GetMapping("/{groupId}/jobs")
    public Result list(@PathVariable(value = "groupId") String groupId
            , @RequestParam(defaultValue = "1") Integer page
            ,@RequestParam(defaultValue = "10") Integer limit){
        BatchJobPo po = new BatchJobPo();
        po.setGroupId(groupId);
        QueryResult<BatchJobPo> result = daoService.selectQueryResult(po,
                page, limit);
        if (result.getTotalrecord() == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(result);
    }

    /**
     * 获取一条任务记录
     * @param id
     * @return
     */
    @ApiOperation(value = "根据ID获取任务信息", httpMethod = "GET")
    @GetMapping("/job/{id}")
    public Result get(@PathVariable String id) {
        BatchJobPo po = new BatchJobPo();
        po.setId(id);
        BatchJobPo batchJobPo = daoService.selectOne(po);
        return ResultUtils.success(batchJobPo);
    }

    /**
     * 增加任务
     * @param po
     * @return
     */
    @ApiOperation(value = "添加任务信息", httpMethod = "POST")
    @PostMapping("/job")
    public Result add(BatchJobPo po){
        //将name赋值给id
        po.setId(po.getName());
        int i = daoService.insert(po);
        if (i == 0) {
            throw new BatchCustomException(ResultEnum.INVALID_REQUEST);
        }
        return ResultUtils.success(ResultEnum.CREATED);
    }

    /**
     * 修改任务
     * @param po
     * @return
     */
    @ApiOperation(value = "修改任务信息", httpMethod = "PUT")
    @PutMapping("/job")
    public Result update(BatchJobPo po){
        int i = daoService.update(po);
        if (i == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(ResultEnum.CREATED);
    }

    /**
     * 删除任务
     * @param id
     * @return
     */
    @ApiOperation(value = "删除任务信息", httpMethod = "DELETE")
    @DeleteMapping("/job/{id}")
    public Result delete(@PathVariable String id){
        //删除step
        BatchStepPo stepPo = new BatchStepPo();
        stepPo.setJobId(id);
        daoService.delete(stepPo);
        //删除job
        BatchJobPo po = new BatchJobPo();
        po.setId(id);
        int i = daoService.delete(po);
        if (i == 0) {
            throw new BatchCustomException(ResultEnum.NOT_FOUND);
        }
        return ResultUtils.success(ResultEnum.DELETED);
    }

}
